<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once(dirname(__FILE__) . '/MY_Model.php');

class Permissions_m extends MY_Model {

	function __construct($db = null, $use_slave = null) {
        parent::__construct($db, $use_slave);
    }

	function getPermissions($params = array()) {

    	if(isset($params['role'])) {
    		$this->db->where('role', $params['role']);
    	}
    	if(isset($params['resource'])) {
    		$this->db->where('resource', $params['resource']);
    	}
    	if(isset($params['privilege'])) {
    		$this->db->where('privilege', $params['privilege']);
    	}
    	if (isset($params['allow'])){
			$this->db->where('allow', $params['allow']);
    	}

    	$query = $this->db->get('admin_acl_permissions');

    	if ($query->num_rows() == 0)
			return false;
		else
    		return $query->result();
    }

    function updatePermission($role = '', $resource = '', $privilege = '', $input = array()) {
		$data = array();
		$data['allow'] = $input['allow'];
		$this->db->where('role', $role);
		$this->db->where('resource', $resource);
		$this->db->where('privilege', $privilege);

		return $this->db->update('admin_acl_permissions', $data);
	}

	function newPermission($role = '', $resource = '', $privilege = '', $input = array()){
		$row = array();
		$row['role'] = $role;
		$row['resource'] = $resource;
		$row['privilege'] = $privilege;
		if (isset($input['allow']))
			$row['allow'] = $input['allow'];
		else
			$row['allow'] = 0;

		if (isset($input['privilege']))
			$row['privilege'] = $input['privilege'];

         $this->db->insert('admin_acl_permissions', $row);
		return $this->db->insert_id();
	}
}

?>